﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.EntityClient;
using OnlineBookStore.OBSModel;
namespace OnlineBookStore.SQLServerDAL
{
    public class User : IDAL.IUser
    {
        /// <summary>
        /// 根据用户名查找用户
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public UserInfo GetUserByUsername(string userName)
        {
            using (OnlineBookStoreEntities obse = new OnlineBookStoreEntities())
            {
                foreach (UserInfo u in obse.UserInfo集)
                {
                    if (u.UserName == userName)
                        return u;
                }

            }
            return null;
        }
        /// <summary>
        /// 根据用户角色查找用户集
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public IList<UserInfo> GetUsersByRole(string roleName)
        {
            IList<UserInfo> userByRole = new List<UserInfo>();


            using (OnlineBookStoreEntities obse = new OnlineBookStoreEntities())
            {
                foreach (UserInfo u in obse.UserInfo集)
                {
                    foreach (Role r in u.Roles)
                    {
                        if(r.RoleName==roleName)
                            userByRole.Add(u);
                    }

                }
            }
            return userByRole;
        }
        /// <summary>
        /// 返回所有用户
        /// </summary>
        /// <returns></returns>
        public IList<UserInfo> GetAllUsers()
        {
            IList<UserInfo> allUsers = new List<UserInfo>();


            using (OnlineBookStoreEntities obse = new OnlineBookStoreEntities())
            {
                foreach (UserInfo u in obse.UserInfo集)
                {
                    allUsers.Add(u);

                }

            }
            return allUsers;
        }
    }
}
